package com.jhws.core.shiro.session;

import com.jhws.business.comm.bo.TblUnitDimBo;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.util.DateUtil;
import com.jhws.util.LogHelper;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/6/30 0030.
 */
public class RedisSessionDao extends EnterpriseCacheSessionDAO {

    @Resource
    private RedisTemplate serializeTemplate;

    LogHelper log;
    /**
     * The Redis key prefix for the sessions
     */
    private String keyPrefix = "shiro_session:";
    private String getKey(String originalKey) {
        return keyPrefix+originalKey;
    }
    @Override
    public void update(Session session) throws UnknownSessionException {
        super.update(session);
        try {
            /* 1分钟更新一次session, 减少redis访问 */
            Long updateTime = (Long)session.getAttribute("updateTime");

            TblUserDimBo user = (TblUserDimBo)session.getAttribute(TblUserDimBo.USER_INFO_REFERENCE);
            if(updateTime!=null && (DateUtil.getTimeStampsNowFmtSec() - updateTime < 60 ) || user == null )
                return;
            //log.d("更新seesion,id="+session.getId().toString());
            session.setAttribute("updateTime",DateUtil.getTimeStampsNowFmtSec());

            serializeTemplate.opsForValue().set(getKey(session.getId().toString()), session,30, TimeUnit.MINUTES);

        } catch (Exception e) {
            log.e(e.getMessage(),e);
        }
    }
    @Override
    public void delete(Session session) {
        log.d("删除seesion,id="+session.getId().toString());
        try {
            String key=getKey(session.getId().toString());
            super.delete(session);
            serializeTemplate.delete(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @Override
    public Collection<Session> getActiveSessions() {
        return super.getActiveSessions();
    }

    @Override
    protected Serializable doCreate(Session session) {
        try {
            super.doCreate(session);
            //log.d("写入session redis :"+getKey(session.getId().toString()));
            serializeTemplate.opsForValue().set(getKey(session.getId().toString()), session,30,TimeUnit.MINUTES);
        } catch (Exception e) {
            log.e(e.getMessage());
        }
        return session.getId();
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        Session session = null;
        try {
            //log.d("doReadSession");
            session = super.doReadSession(sessionId);
            if(session == null || session.getAttribute(TblUserDimBo.USER_INFO_REFERENCE) == null) {
                //log.d("read redis");
                session = (Session) serializeTemplate.opsForValue().get(getKey(sessionId.toString()));
                if(session != null)
                    super.update(session);
                return session;
            }
        } catch (Exception e) {
            log.e(e.getMessage());
        }
        return session;
    }
}
